<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="角色名称"></el-input>
      </el-form-item>
      <el-form-item label="角色编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="角色编码"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio
            v-for="item in dataForm.statusList"
            :key="item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { SaveRoleApi } from "@/api/role";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        code: "",
        remark: "",
        status: 1,
        statusList: [{ label: "正常", value: 1 }, { label: "停用", value: 2 }]
      },
      dataRule: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        code: [{ required: true, message: "角色编码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    init(item, isAdd) {
      if (!isAdd) {
        this.dataForm.id = item.id;
        this.dataForm.name = item.roleName;
        this.dataForm.code = item.code;
        this.dataForm.remark = item.remark;
        this.dataForm.status = item.status;
      } else {
        this.dataForm.id = null;
        this.dataForm.name = null;
        this.dataForm.code = null;
        this.dataForm.remark = null;
        this.dataForm.status = 1;
      }
      this.visible = true;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          SaveRoleApi(this.dataForm)
            .then(res => {
              window.console.log(res);
              if (res.data.code == 200) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              } else {
                this.$message.error(res.data.message);
              }
            })
            .catch(err => {
              window.console.log(err);
              this.$message.error(err.message);
            });
          return;
        }
      });
    }
  }
};
</script>
